package com.agency8x.controller;

import com.agency8x.bean.JsonResultBean;
import com.agency8x.interceptor.CheckLoginInterceptor;
import com.agency8x.model.AdminModel;
import com.agency8x.validator.AdminValidator;
import com.agency8x.web.util.ControllerUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

/**
 * 权限规则页控制器
 * 
 * @author SoChishun
 *
 */
@Before(CheckLoginInterceptor.class)
public class AuthRuleController extends Controller {
	public void index() {
		String search_word = this.getPara("word");//搜索
		setAttr("listPage", AdminModel.me.paginate(getParaToInt(0, 1), 10, search_word));
		// 设置传入分页的url参数
		if (StrKit.notBlank(search_word)) {
			this.setAttr("append", "?word=" + search_word);
		}
		this.render("index.html");
	}

	public void edit() {
		Integer id = this.getParaToInt();
		if (null != id && id > 0) {
			AdminModel model = AdminModel.me.findById(id);
			this.setAttr("itemData", model);
		}
		this.render("edit.html");
	}

	@Before(AdminValidator.class)
	public void doSave() {
		Integer id=this.getParaToInt("id");
		boolean result=false;
		if(null==id){
			result=getModel(AdminModel.class, "").save();	
		}else{
			result=getModel(AdminModel.class, "").update();
		}
		if(result){
			ControllerUtil.renderDisptchJumpSuccess(this); // 操作成功，倒计时3秒后自动刷新列表页面
		}else{
			ControllerUtil.renderDisptchJumpError(this, "保存失败"); // 操作失败，倒计时3秒后，自动后退到提交的表单页面
		}
	}

	public void profile() {
		this.render("profile.html");
	}

	public void save_profile() {
		this.renderText("save!!!");
	}

	/**
	 * 删除操作
	 * <br />支持单个删除和批量删除
	 * <br />单个删除：?id=1
	 * <br />批量删除：?id=1,2,3
	 * @since 1.0 2017-5-15
	 */
	public void doDelete() {
		boolean result = false;
		String id = this.getPara("id");
		if (null != id) {
			result = AdminModel.me.deleteById(id);
		}
		this.renderJson(new JsonResultBean(result, "删除" + (result ? "成功" : "失败")));
	}
}
